package com.qf.mail;

import com.qf.utils.MyMailUtils;
import com.qf.vo.ResultVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

@RestController
@RequestMapping("/mail")
public class MaiController {


    public static final String MAIL_FILE_PATH = "D:\\00qd-2304\\mail\\";

    /**
     * 发送邮件接口
     * @param receiver 收件人
     * @param title 邮件标题
     * @param content 邮件内容
     * @param files 附件
     * @return
     */
    @PostMapping("/send")
    public ResultVo sendMail(String[] receiver, String title, String content, MultipartFile[] files) throws IOException {

        File[] fileArr = new File[files.length];

        // MultipartFile[] --->  File[]
        // 需要把前端发送的文件 先保存到服务器端的某个目录下
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];

            String originalFilename = file.getOriginalFilename();
            String filepath = MAIL_FILE_PATH + originalFilename;
            // 保存文件到服务器端
            file.transferTo(new File(filepath));
            // 把文件放入文件数组中
            fileArr[i] = new File(filepath);
        }

        MyMailUtils.sendMail( Arrays.asList(receiver),title,content,fileArr);

        return new ResultVo(1, "ok", null);
    }

}
